<!DOCTYPE html>
<html>
	<head>
		<title>表格显示示例-修改</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<style type="text/css">
			/* 使其整个背景变白 */
			body{background-color: #FFF;}
			.c-panel{box-shadow: 0 0 0;}
			/* 调整一下表单元素尺寸 */
			/* .c-panel .el-form-item__label{width: 120px;}
			.c-panel .el-form .el-input{width: 250px;} */
		</style>
	</head>
	<body>
		<!-- 防止margin向下击穿 -->
		<div style="margin-top: -1em;" title="防止margin向下击穿"><br></div>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- 参数栏 -->
				<div class="c-title" v-if="id == 0">数据添加</div>
				<div class="c-title" v-else>数据修改</div>
				<el-form v-if="m">
					<div class="c-item br">
						<label class="c-label">编号：</label>
						<el-input size="mini" v-model="m.id" disabled></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">名称：</label>
						<el-input size="mini" v-model="m.name"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">图标：</label>
						<el-input size="mini" v-model="m.icon"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">状态：</label>
						<el-radio-group size="mini" v-model="m.status">
							<el-radio :label="1">正常</el-radio>
							<el-radio :label="2">禁用</el-radio>
						</el-radio-group>
					</div>
					<div class="c-item br">
						<label class="c-label"></label>
						<el-button size="mini" type="primary" icon="el-icon-plus" @click="ok()">确定</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<script type="text/javascript">
			// 创建一个空的 model
			function create_m() {
				return {
					id: 0, 
					name: '', 
					icon: '',
					avatar: 'http://color-test.oss-cn-qingdao.aliyuncs.com/dyc/img/2020/01/19/15794196587511189314194.png', 
					money: 99.99,
					status: 1,
					create_time: new Date()
				}
			}
		</script>
		<script src="mock-data-list.js"></script>
		<script type="text/javascript">
			new Vue({
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),	// 获取传递的参数 
					m: null
				},
				methods: {
					// 表单验证  
					submit_check: function() {
						var m = this.m;
						if(sa.isNull(m.name)) {
							return sa.error('请输入名称');
						}
						if(sa.isNull(m.icon)) {
							return sa.error('请输入图标');
						}
						return 'ok';
					},
					// 提交 
					ok: function() {
						// 表单验证 
						if(this.submit_check() != 'ok') {
							return;
						}
						// 开始增加或修改
						if(this.id == 0) {
							// id == 0 为增加        
							sa.ajax2('/data/add', this.m, function(res) {
								sa.alert('增加成功', function() {
									// this.m = create_m();
									parent.app.f5();		// 刷新父页面列表 
									sa.closeCurrIframe();	// 关闭本页 
								}); 
							});
						} else {
							// id != 0 为修改  
							sa.ajax2('/data/update', this.m, function(res){
								sa.alert('修改成功', function(){
									parent.app.f5();		// 刷新父页面列表 
									sa.closeCurrIframe();	// 关闭本页 
								}); 
							});
						}
					},
				},
				created: function(){
					// 如果是添加
					if(this.id == 0){
						this.m = create_m();
					} else {
						// id 非 0 代表 是要修改 先查询一下，把旧值显示在input中  
						sa.ajax2('/data/getById', {id: this.id}, function(res) {
							this.m = res.data;
						}.bind(this), {res: getMockData()});
					}
				}
			})
			
			// 遍历判断， 获取模拟数据  
			function getMockData() {
				var id = sa.p('id', 0);	
				var data = null;
				// 遍历判断 
				mockDataList.data.forEach(function(item) {
					if(item.id == id) {
						data = item;
					}
				})
				// 创建模拟数据 
				var mockData = {
					code: 200,
					msg: 'ok',
					data: data
				}
				return mockData;
			}
		</script>
		
	</body>
</html>
